﻿using System;
using UnityEngine;

// Token: 0x0200005E RID: 94
public class CameraRenderImageToTex : MonoBehaviour
{
	// Token: 0x06000306 RID: 774 RVA: 0x0001406C File Offset: 0x0001226C
	private void Awake()
	{
		this.CreateRenderTex();
	}

	// Token: 0x06000307 RID: 775 RVA: 0x00014074 File Offset: 0x00012274
	private void LateUpdate()
	{
		if (this.ssCam != null && Input.GetKeyDown(this.ssKey))
		{
			ScreenShotCamera screenShotCamera = UnityEngine.Object.Instantiate<ScreenShotCamera>(this.ssCam);
			screenShotCamera.Shot(this.Tex);
		}
	}

	// Token: 0x06000308 RID: 776 RVA: 0x000140BC File Offset: 0x000122BC
	private void OnRenderImage(RenderTexture source, RenderTexture destination)
	{
		if (this.Tex == null)
		{
			return;
		}
		this.CheckScreenSize();
		if (this.material != null)
		{
			if (!this.isCameraTarget)
			{
				Graphics.Blit(source, this.Tex, this.material);
			}
			Graphics.Blit(source, destination, this.material);
		}
		else
		{
			if (!this.isCameraTarget)
			{
				Graphics.Blit(source, this.Tex);
			}
			Graphics.Blit(source, destination);
		}
	}

	// Token: 0x06000309 RID: 777 RVA: 0x00014140 File Offset: 0x00012340
	private void CheckScreenSize()
	{
		if (this.Tex.width != Screen.width || this.Tex.height != Screen.height)
		{
			this.CreateRenderTex();
		}
	}

	// Token: 0x0600030A RID: 778 RVA: 0x00014174 File Offset: 0x00012374
	private void CreateRenderTex()
	{
		if (this.Tex != null)
		{
			this.Tex.Release();
			UnityEngine.Object.Destroy(this.Tex);
		}
		this.Tex = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGB32);
		this.Tex.filterMode = FilterMode.Point;
		this.Tex.wrapMode = TextureWrapMode.Clamp;
		if (this.isCameraTarget)
		{
			base.GetComponent<Camera>().targetTexture = this.Tex;
		}
	}

	// Token: 0x0600030B RID: 779 RVA: 0x000141F4 File Offset: 0x000123F4
	private void Destroy()
	{
		if (this.Tex)
		{
			this.Tex.Release();
			UnityEngine.Object.Destroy(this.Tex);
			this.Tex = null;
		}
		if (this.isCameraTarget)
		{
			base.GetComponent<Camera>().targetTexture = null;
		}
	}

	// Token: 0x0600030C RID: 780 RVA: 0x00014248 File Offset: 0x00012448
	public Texture2D GetTex2D()
	{
		RenderTexture.active = this.Tex;
		Texture2D texture2D = new Texture2D(this.Tex.width, this.Tex.height, TextureFormat.RGB24, false);
		texture2D.filterMode = FilterMode.Point;
		texture2D.wrapMode = TextureWrapMode.Clamp;
		texture2D.ReadPixels(new Rect(0f, 0f, (float)this.Tex.width, (float)this.Tex.height), 0, 0, false);
		texture2D.Apply();
		return texture2D;
	}

	// Token: 0x040002BE RID: 702
	public RenderTexture Tex;

	// Token: 0x040002BF RID: 703
	public bool isCameraTarget;

	// Token: 0x040002C0 RID: 704
	public Material material;

	// Token: 0x040002C1 RID: 705
	public ScreenShotCamera ssCam;

	// Token: 0x040002C2 RID: 706
	public KeyCode ssKey = KeyCode.F11;
}
